<head>
	<link rel="stylesheet" href="css/data.css" type="text/css" />
</head>

<?php
	require_once("../dart/phpChart/conf.php");
	include "vars.php";
	include "data_include.php";

	if(isset($_GET['player_id'])){
		$selected = true;
	} else {
		$selected = false;
	}

	print("<h1>MMS-Dart-Liga</h1>");
	
	print("<a href='index.php'>Spieldaten</a><br/></br/>");
	
	print("<table class='gridtable' style='float:left;margin-right: 10px;'>");
	print("<th>Spieler</th>");
	print("<th></th>");
	print("<form action='".$_SERVER['PHP_SELF']."'>");
	$res = $mysqli->query("select * from players order by firstName");
	$chart_data = array();
	$series = array();
	while($row=$res->fetch_assoc()) {
		$checked = false;
		if ($selected && is_array($_GET['player_id'])) {
			foreach($_GET['player_id'] as $value){
				if($row['id']==$value) {
					$checked = true;
					$week = date('W');
					$week = $week - 52;
					$year = date('Y');
					$last = 0;
					$chart_data_player = array();
					for ($i=$week;$i<$week+52;$i++) {
						$found = false;
						$res2 = $mysqli->query("select * FROM view_score_kw_winner where player_id = ".$row["id"]." and kw=".$i." and year=".$year." order by year,kw");
						while($row2=$res2->fetch_assoc()) {
							$last = $row2["score_abs"];
							$chart_data_player[] = $row2["score_abs"]*1;
							$found = true;
						}
						if($found == false) {
							$chart_data_player[] = $last*1;
						}
					}
					$series[] = array('label'=>$row["firstName"],'showLabel'=>true);
					$chart_data[] = $chart_data_player;
				}
			}
		}
		print("<tr>");
		print("<td>".$row['firstName']." ".$row['lastName']."</td>");
		print("<td>");
		if($checked) {
			print("<input type='checkbox' name='player_id[]' onclick='submit()' checked='checked' value='".$row['id']."' />");
		} else {
			print("<input type='checkbox' name='player_id[]' onclick='submit()' value='".$row['id']."' />");
		}
		print("</td>");
		print("</tr>");
		
	}
	print("</form>");
	print("</table>");
	
	if($selected) {
		print("<div id='chart' style='float:left;'>");
		
		$pc = new C_PhpChartX($chart_data,'basic_chart');
	
		$pc->set_title(array('text'=>'Punkte pro Woche'));
		$pc->add_plugins(array('cursor'));
		$pc->set_animate(true);
		$pc->set_cursor(array(
				'show'=>true,
				'zoom'=>true));
		$pc->set_legend(array('show'=>true));
		for($i=0;$i<count($series);$i++){
			$pc->add_series($series[$i]);
		}
		$pc->set_axes(array(
				'xaxis'=> array('label'=>'Woche'),
				'yaxis'=> array('label'=>'Punkte')
			));
		$pc->draw(800,500,null,'cart');
		print("</div>");
	}
?>